package cn.edu.zut.hkxy.controller;

import cn.edu.zut.hkxy.pojo.Admin;
import cn.edu.zut.hkxy.pojo.AjaxResult;
import cn.edu.zut.hkxy.pojo.Jobs;
import cn.edu.zut.hkxy.service.JobsService;
import cn.edu.zut.hkxy.util.FileUploadUtils;
import cn.edu.zut.hkxy.util.HTMLUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/jobs")
public class JobsController {
    @Autowired
    private JobsService service;
    @RequestMapping("/publish")
    public String toPublish(Model model){
        model.addAttribute("type","jobs");
        return "ArticlePublish";
    }

    @RequestMapping("/do-publish")
    @ResponseBody
    public AjaxResult doPublish(@RequestParam(required = false,value = "file") MultipartFile file,
                                @RequestParam(required = false,value = "cover") MultipartFile cover,
                                @RequestParam Integer state,Jobs jobs,HttpSession session){
        AjaxResult result=new AjaxResult();
        try {
            String filename=file.getOriginalFilename();
            if (filename==null||filename.equals("")){
                jobs.setDocUrl(null);
                jobs.setDocName(null);
            }else{
                //获取附件
                jobs.setDocName(filename);
                jobs.setDocUrl(FileUploadUtils.saveFile(file));
            }
            String covername=cover.getOriginalFilename();
            if(covername==null||covername.equals("")){
                jobs.setImgPreview(null);
            }else{
                jobs.setImgPreview(FileUploadUtils.saveImgGetPath(cover).get("url"));
            }
            //判断发布1还是草稿0
            if (state==1){
                jobs.setState(1);
            }else if (state==0){
                jobs.setState(0);
            }else {
                jobs.setState(2);
            }
            //获取作者
            Admin admin=(Admin) session.getAttribute("admin");
            jobs.setAuthor(admin.getRealname());

            service.add(jobs);

            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }
        finally {
            return result;
        }
    }
    @RequestMapping("/manage")
    public String toManage(Model model){
        model.addAttribute("type","jobs");
        return "ArticleManage";
    }

    @RequestMapping("/page-list-json")
    @ResponseBody
    public AjaxResult getPage(Model model,@RequestParam Integer pageNum,@RequestParam String state,@RequestParam String date,@RequestParam String title){
        AjaxResult result=new AjaxResult();
        try {
            List<Jobs> list=service.getPage(pageNum,state,date,title);
            result.setData(list);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }
        finally {
            return result;
        }
    }

    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Integer id){
        AjaxResult result=new AjaxResult();
        try {
            service.deleteById(id);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }
        finally {
            return result;
        }
    }

    @RequestMapping("/show/{id}")
    public String toShowById(@PathVariable int id, Model model) throws JsonProcessingException {
        Jobs jobs= service.getById(id);
        jobs.setContent(HTMLUtils.htmlEncode(jobs.getContent()));
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(jobs);
        System.out.println(json);
        model.addAttribute("json",json);
        model.addAttribute("type","jobs");
        return "showArticle";
    }

    @RequestMapping("/show-one-json")
    @ResponseBody
    public AjaxResult toShow(@RequestParam int id){
        AjaxResult result=new AjaxResult();
        try {
            result.setData(service.getById(id));
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
            service.hitsAdd(id);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }
        finally {
            return result;
        }
    }

    @RequestMapping("/publish-only")
    @ResponseBody
    public AjaxResult doPublish(@RequestParam Integer id){
        AjaxResult result=new AjaxResult();
        try {
            Jobs jobs=new Jobs();
            jobs.setId(id);
            jobs.setState(1);
            service.modify(jobs);
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }
        finally {
            return result;
        }
    }

    @RequestMapping("/modify")
    public String doModify(@RequestParam Integer id,Model model)throws Exception{
        Jobs jobs=service.getById(id);
        jobs.setContent(HTMLUtils.htmlEncode(jobs.getContent()));
        ObjectMapper objectMapper=new ObjectMapper();
        String json = objectMapper.writeValueAsString(jobs);
        model.addAttribute("json",json);
        model.addAttribute("type","jobs");
        return "ArticleModify";
    }

    @RequestMapping("/do-modify")
    @ResponseBody
    public AjaxResult doModify(@RequestParam(required = false,value = "file") MultipartFile file,
                                @RequestParam Integer state,Jobs jobs,HttpSession session,@RequestParam Integer id){
        AjaxResult result=new AjaxResult();
        try {
            String filename=file.getOriginalFilename();
            if (filename==null||filename.equals("")){
                jobs.setDocUrl(null);
                jobs.setDocName(null);
            }else{
                jobs.setDocName(filename);
                jobs.setDocUrl(FileUploadUtils.saveFile(file));
            }
            //发布1，草稿0
            if (state==1){
                jobs.setState(1);
            }else if (state==0){
                jobs.setState(0);
            }else {
                jobs.setState(2);
            }
            //获取作者
            Admin admin=(Admin) session.getAttribute("admin");
            jobs.setAuthor(admin.getRealname());
            jobs.setId(id);
            service.modify(jobs);

            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }
        finally {
            return result;
        }
    }

    @RequestMapping("/pageBean-json")
    @ResponseBody
    public AjaxResult getPageBean(@RequestParam Integer currentPageNum,@RequestParam Integer pageSize){
        AjaxResult result=new AjaxResult();
        try {
            result.setData(service.getPageBean(currentPageNum,pageSize));
            result.setCode(AjaxResult.RESULT_SUCCESS_CODE);
        }catch (Exception e){
            result.setCode(AjaxResult.RESULT_ERROR_CODE);
            result.setMessage(e.getMessage());
        }
        finally {
            return result;
        }
    }
}
